<!DOCTYPE html>
<html>
<head>
<title>用户管理</title>
<link href="../../../common/all.css" rel="stylesheet">
<script type="text/javascript" src="../../../common/all.js"></script>
<script type="text/javascript" src="../../../common/util/menuAuthorization.js"></script>

<link rel="stylesheet" href="../../../common/style/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="../../../common/jquery/jquery.ztree/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="../../../common/jquery/jquery.ztree/jquery.ztree.excheck-3.5.js"></script>
	

<style type="text/css">

.form-actions{
     padding-right: 1000px;
}
#messageBox{
 color: red;
}
</style>


<script type="text/javascript">

	$(document).ready(function() {
		
		$("#inputForm").validate({
			submitHandler: function(form){
				//loading('正在提交，请稍等...');
				 addRole();
				//form.submit();
			},
			errorContainer: "#messageBox",
			errorPlacement: function(error, element) {
				$("#messageBox").text("输入有误，请先更正。");
				if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
					error.appendTo(element.parent().parent());
				} else {
					error.insertAfter(element);
				}
			}
		});
	});
	
	
</script>

</head>

<body style="overflow-x: hidden; overflow-y: hidden;">
	
<!-- 	<ul class="nav nav-tabs"> -->
<!-- 		<li > -->
<!-- 			<a href="rolelist.html">角色列表</a> -->
<!-- 		</li> -->
<!-- 		<li class="active"> -->
<!-- 			<a href="roleAdd.html">修改角色</a> -->
<!-- 		</li> -->
<!-- 	</ul> -->
           
 <div class="addClz">
             
      <form id="inputForm" action="" class="form-horizontal" role="form">

             <input  name="id"  id="_roleid" type="hidden" />
  
            <div class="form-group">
               <label class="col-sm-1 control-label" > <span class="help-inline"><font color="red">*</font> </span>角色名称： 
               </label>
               <div class="col-sd-1">
                  <input class="required input-xlarge" maxlength="20"  name="name"  id="_roleName" type="text" />
               </div>
         
            </div>
            
            
            
            
             <div class="form-group">
               <label class="col-sm-1 control-label " >
               <span class="help-inline"><font color="red">*</font>状态：</label>
               <div class="col-sd-1">
			      &nbsp;  &nbsp;&nbsp;启用 <input type="radio" name="status" value="0" checked="checked"/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;禁用<input type="radio" name="status" value="1"/>
               </div>
            </div>
            
                 
            <div class="form-group">
               <label class="col-sm-1 control-label ">角色类型：</label>
               <div class="col-sd-1">
                  <select name="types" id="_type">
<!--                   <option value="1">系统管理角色</option> -->
<!--                   <option value="2">商家角色</option> -->
<!--                   <option value="3">政府角色</option> -->
                  </select>
               </div>
            </div>
            
            
              <div class="form-group">
               <label class="col-sm-1 control-label ">权限分配：</label>
               <div class="col-sd-1" id="_permission_plan_div">
               
				&nbsp;
				<input id="citySel" type="text" readonly value="" placeholder="点击选择权限" style="width:220px;" onclick="showMenu(); return false;"/>
				<input id="areaCodeV" type="hidden" >
				<div id="menuContent" class="menuContent" style="display:none; width:600px; height:800px;z-index:9999, position: absolute; background:rgb(242, 242, 242);OVERFLOW-Y: auto; OVERFLOW-X:hidden; ">
		        <!--  <a href="javascript:claclArea()">清空</a> -->   
		         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp;
		         &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp;
		         <a href="javascript:checkAllNodes()" >全选</a>
		            &nbsp; &nbsp; &nbsp; &nbsp; 
		         <a href="javascript:cancelAllNodes()" >全取消</a>
		             &nbsp; &nbsp; &nbsp; &nbsp; 
		          <a href="javascript:expandAll(true)" >展开</a>
		            &nbsp; &nbsp; &nbsp; &nbsp; 
		         <a href="javascript:expandAll(false)" >折叠</a>
		             &nbsp; &nbsp; &nbsp; &nbsp; 
		         <a href="javascript:enterSel()">确定</a> 
			       <ul id="treeDemo" class="ztree" style="margin-top:0; width:160px;"></ul>
		        </div>
			
               </div>
            </div>
            
            <div class="form-group">
               <label class="col-sm-1 control-label " >
                                            备注：</label>
               <div class="col-sm-1">
               <textarea rows="6" cols="60"  maxlength="200" name="remake" id="_remake"></textarea>
               </div>
            </div>
            
         
            <div class="form-actions">
			<input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		    </div>    
         </form>
         
    </div>
		
	<script type="text/javascript">

	
	
	
	function addRole(){
			var roleObj=$("#inputForm").serialize();
			var menuIds=getCheckedAll();
		   // alert(JSON.stringify(menuIds))
			var menuStr="";
			if(menuIds!=""&&menuIds.length>0){
				for(var i=0;i<menuIds.length;i++){
					var menuId=menuIds[i];
					menuStr+="&menuIds["+i+"]="+menuId;
				}
			}
			//menuStr=encodeURI(menuStr);
			roleObj+=menuStr;
// 	    	console.log(roleObj);
			$.ajax({
				url : getContextPath()+"/sys/role/update",
				type : "POST",
				//contentType: "application/json; charset=utf-8",
				dataType : "json",
				data :roleObj,
				success : function(returnData,status,XMLHttpRequest)
				{	
				    resolveResultBeanIsOk(returnData,status);
				},
				error : function() {
					alert("请求异常！");
				}
			});
			
		}
	
	
	
	//初始化
	$(function(){
		initUpdate();
	})
	

	

	function initUpdate(){
		var id=getUrlParam("id");
		//alert(id);
		if(id==null||id==""){
			alert("参数异常，无法修改！");
			return;
		}
		$("#_roleid").val(id);
		//请求数据
		$.ajax({
			url : getContextPath()+"/sys/role/get",
			type : "get",
			//contentType: "application/json; charset=utf-8",
			dataType : "json",
			data :{"id":id},
			success : function(returnData,status,XMLHttpRequest)
			{	
			   if(resolveResultBeanIsError(returnData,status))
			   {
				   
				  var data=returnData.data;
				  $("#_roleName").val(data.name);
				  
				  //状态redio
				  var status=data.status;
				  if(status==0){
					  $("input[name=status]:eq(0)").attr("checked",'checked'); 
				  }else if(status==1)
				  {
					  $("input[name=status]:eq(1)").attr("checked",'checked'); 
 
				  }
				  
				 
				  //初始字典
		           initDictByType('role_type',"_type",data.types);
				  
				  //角色类型
				 // $("#_type").attr("value",data.types);
				  //$("#_type option[value='"+data.types+"']").attr("selected", "selected");  

				  //权限
			      var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
			      if(treeObj==null){
			    	  //延迟
			    	  setTimeout(function () { 
			    		  checkZTree(null,data.menus);
			    	    }, 500);
			      }else{
			    	  checkZTree(treeObj,data.menus);
			      }
				  //权限 end
				  
				  
				  
				  
				  $("#_remake").val(data.remake);
				   
			   }
			},
			error : function() {
				alert("请求异常！");
			}
		});
		
	}
	

	function checkZTree(treeObj,menus){
		
		if(treeObj==null){
			 treeObj = $.fn.zTree.getZTreeObj("treeDemo");
		}
	      

	      var nodes= treeObj.getNodes();
//	      console.log(nodes);
//	      console.log(menus);
	      
	      if(menus!=""&&menus.length>0)
	      {
	    	  for (var i=0, l=nodes.length; i < l; i++) 
		      {//for ztree
	    		  var zTreeId=nodes[i].id;
		          //for children
	    		  var childrenNodes=nodes[i].children;
//		          console.log(childrenNodes);
	    		  if(typeof(childrenNodes)!="undefined"&&childrenNodes!=""&&childrenNodes.length>0)
	    		  {//有子节点的情况
	    			  for(var ch=0;ch<childrenNodes.length;ch++)
	    			  {
//	    				  console.log(childrenNodes[ch]);
			    		  var zTreeIdx=childrenNodes[ch].id;
			    		  for(var m=0;m<menus.length;m++)
				    	  {//for menus data 
				    		 var menuId=menus[m].id;
				    		  if(zTreeIdx==menuId)
				    		  {
				    			  treeObj.checkNode(childrenNodes[ch], true, true);
				    		  }
				    	  }

	    			  }
	    			  
	    		  }else
	    		  {//没有子节点的情况
	    			  // alert(zTreeId);
			    	  for(var m=0;m<menus.length;m++)
			    	  {//for menus data 
			    		 var menuId=menus[m].id;
			    		  if(zTreeId==menuId)
			    		  {
			    			  treeObj.checkNode(nodes[i], true, true);
			    		  }
			    	  }
	    		  }
		      }
	      }
	}
	
	
	</script>	
	
	


</body>
</html>